package com.ludomancy.sos64.components;

import java.awt.Graphics;
import java.awt.Point;

import com.ludomancy.gamelib.ImageAnimation;
import com.ludomancy.gamelib.ImageStrip;
import com.ludomancy.gamelib.Randomizer;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.Sound;
import com.ludomancy.sos64.Component;
import com.ludomancy.sos64.Game;
import com.ludomancy.sos64.Sos64App;
import com.ludomancy.sos64.Terrain;

public class Dam extends Component
{
	private ImageAnimation blockade;
	private int consumption = 100;
	private boolean wasConsumedLastTurn = false;
	
	public Dam(Point pos)
	{
		super(pos.x,pos.y);
		if(blockade == null)
		{
			blockade = new ImageAnimation(new ImageStrip(ResourceManager.getImage("blockade.png"), 32, 67), 4);
		}
	}
	
	public boolean canMove()
	{
		return false;
	}
	
	public Point getSize()
	{
		return new Point(32,67);
	}

	public void paint(Graphics g, int px, int py)
	{
		if(wasConsumedLastTurn)
		{
			py += Randomizer.getRandom(-1,1);
		}
		blockade.paintCurrentFrame(g, px, py);
		
		wasConsumedLastTurn = false;
	}

	public void update()
	{
		blockade.update();
		if(isConsumed())
		{
			// break dam
			Sound.play("collapse.wav");
			Game.game.terrain.depth[2][9] = Terrain.DEPTH_LOW;
			Game.game.terrain.updateTerrain();
			Game.game.components.remove(this);
			Sos64App.mainWidget.world.startShake();
		}
	}
	
	public boolean isConsumed()
	{
		return consumption == 0;
	}

	public void consume()
	{
		wasConsumedLastTurn = true;
		consumption = Math.max(0,consumption-1);
	}
}
